home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH09 / PGM9_1.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-02-08  |  2.3 KB  |  146 lines

  1. ; Pgm9_1.ASM
  2. ;
  3. ; Several examples demonstrating how to convert various
  4. ; arithmetic expressions into assembly language.
  5.  
  6.         .xlist
  7.         include     stdlib.a
  8.         includelib    stdlib.lib
  9.         .list
  10.  
  11.  
  12. dseg        segment    para public 'data'
  13.  
  14. ; Arbitrary variables this program uses.
  15.  
  16. u        word    ?
  17. v        word    ?
  18. w        word    ?
  19. x        word    ?
  20. y        word    ?
  21.  
  22. dseg        ends
  23.  
  24.  
  25.  
  26. cseg        segment    para public 'code'
  27.         assume    cs:cseg, ds:dseg
  28.  
  29.  
  30. ; GETI-    Reads an integer variable from the user and returns its
  31. ;    its value in the AX register.
  32.  
  33. geti        textequ    <call _geti>
  34. _geti        proc
  35.         push    es
  36.         push    di
  37.  
  38.         getsm
  39.         atoi
  40.         free
  41.  
  42.         pop    di
  43.         pop    es
  44.         ret
  45. _geti        endp
  46.  
  47.  
  48. Main        proc
  49.         mov    ax, dseg
  50.         mov    ds, ax
  51.         mov    es, ax
  52.         meminit
  53.  
  54.  
  55.         print
  56.         byte    "Abitrary expression program",cr,lf
  57.         byte    "---------------------------",cr,lf
  58.         byte    lf
  59.         byte    "Enter a value for u: ",0
  60.  
  61.         geti
  62.         mov    u, ax
  63.  
  64.         print
  65.         byte    "Enter a value for v: ",0
  66.         geti
  67.         mov    v, ax
  68.  
  69.         print
  70.         byte    "Enter a value for w: ",0
  71.         geti
  72.         mov    w, ax
  73.  
  74.         print
  75.         byte    "Enter a non-zero value for x: ",0
  76.         geti
  77.         mov    x, ax
  78.  
  79.         print
  80.         byte    "Enter a non-zero value for y: ",0
  81.         geti
  82.         mov    y, ax
  83.  
  84.  
  85. ; Okay, compute Z := (X+Y)*(U+V*W)/X and print the result.
  86.  
  87.         print
  88.         byte    cr,lf
  89.         byte    "(X+Y) * (U+V*W)/X is ",0
  90.  
  91.         mov    ax, v        ;Compute V*W
  92.         imul    w        ; and then add in
  93.         add    ax, u              ; U.
  94.         mov    bx, ax        ;Save in a temp location for now.
  95.  
  96.         mov    ax, x        ;Compute X+Y, multiply this
  97.         add    ax, y        ; sum by the result above,
  98.         imul    bx        ; and then divide the whole
  99.         idiv    x        ; thing by X.
  100.  
  101.         puti
  102.         putcr
  103.  
  104. ; Compute ((X-Y*U) + (U*V) - W)/(X*Y)
  105.  
  106.         print
  107.         byte    "((X-Y*U) + (U*V) - W)/(X*Y) = ",0
  108.  
  109.         mov    ax, y        ;Compute y*u first
  110.         imul    u
  111.         mov    dx, X        ;Now compute X-Y*U
  112.         sub    dx, ax
  113.         mov    cx, dx        ;Save in temp
  114.  
  115.         mov    ax, u        ;Compute U*V
  116.         imul    V
  117.         add    cx, ax        ;Compute (X-Y*U) + (U*V)
  118.  
  119.         sub    cx, w        ;Compute ((X-Y*U) + (U*V) - W)
  120.  
  121.         mov    ax, x        ;Compute (X*Y)
  122.         imul    y
  123.  
  124.         xchg    ax, cx
  125.         cwd            ;Compute NUMERATOR/(X*Y)
  126.         idiv    cx
  127.  
  128.         puti
  129.         putcr
  130.  
  131.  
  132.  
  133. Quit:        ExitPgm            ;DOS macro to quit program.
  134. Main        endp
  135.  
  136. cseg        ends
  137.  
  138. sseg        segment    para stack 'stack'
  139. stk        byte    1024 dup ("stack   ")
  140. sseg        ends
  141.  
  142. zzzzzzseg    segment    para public 'zzzzzz'
  143. LastBytes    byte    16 dup (?)
  144. zzzzzzseg    ends
  145.         end    Main
  146.